package com.zilu.digitaltwin.entity.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author :heguangyao
 * @Date :2023/1/5 9:11
 **/
@Data
public class CatalogDetailDTO {

    private String code;

    private String name;

    private String desc;

    /**
     * 二维表信息，如果有数据，columns作为表头，contents其后为表数据
     */
    private TableInfo tableInfo;

    public static List<CatalogDetailDTO> toDetailList(String catalogDetailStr) {
        if (StringUtils.isBlank(catalogDetailStr)) {
            return null;
        }
        return JSONObject.parseObject(catalogDetailStr, new TypeReference<ArrayList<CatalogDetailDTO>>() {
        });
    }

    public static String toCatalogDetailStr(List<CatalogDetailDTO> catalogDetailDTOList) {
        if (CollectionUtils.isEmpty(catalogDetailDTOList))
            return null;
        return JSON.toJSONString(catalogDetailDTOList);
    }

    @Data
    public static class TableInfo {
        private List<String> columns;
        private List<List<String>> contents;

        public List<String> getColumns() {
            if (null == columns) {
                columns = new ArrayList<>();
            }
            return columns;
        }

        public List<List<String>> getContents() {
            if (null == contents){
                contents = new ArrayList<>();
            }
            return contents;
        }
    }


}
